<template>
	<view class="ys">
		<view class="title">
			上传截图(可选)
		</view>
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="index">
				<image :src="item" mode="aspectFill"></image>
			</view>
			<view class="item" @click="uploadImg">
				<image src="/static/index/add.png" mode="aspectFill"></image>
			</view>
			<button class="btn" @click="submit" :loading="loading" :disabled="loading">
				提交
			</button>
		</view>
	</view>
</template>

<script>
	import {
		acceptConfirm
	} from "@/api/shop.js"
	import {
		offerConfirm
	} from "@/api/offer.js"
	export default {
		data() {
			return {
				list: [],
				idlist: [],
				id: '',
				type: '',
				loading: false
			}
		},
		onLoad(e) {
			if (e.id) this.id = e.id
			if (e.type) this.type = e.type
		},
		methods: {
			submit() {
				this.loading = true
				if (this.type == 'offer') {
					offerConfirm({
						id: this.id,
						images: this.idlist
					}).then(data => {
						uni.$showMsg(data.msg)
						if (data.code == 0) {
							let pages = getCurrentPages(); //获取所有页面栈实例列表
							let nowPage = pages[pages.length - 1]; //当前页页面实例
							let prevPage = pages[pages.length - 2]; //上一页页面实例
							prevPage.$vm.isActive = 2; //修改上一页data里面的tagIndex 参数值
							uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
								delta: 1
							});
						} else {
							that.loading = false
						}
					})
				} else {
					acceptConfirm({
						id: this.id,
						images: this.idlist
					}).then(data => {
						uni.$showMsg(data.msg)
						if (data.code == 0) {
							let pages = getCurrentPages(); //获取所有页面栈实例列表
							let nowPage = pages[pages.length - 1]; //当前页页面实例
							let prevPage = pages[pages.length - 2]; //上一页页面实例
							prevPage.$vm.isActive = 2; //修改上一页data里面的tagIndex 参数值
							uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
								delta: 1
							});
						} else {
							that.loading = false
						}
					})
				}

			},
			uploadImg() {
				var that = this
				uni.chooseImage({
					success(res) {
						let igmFile = res.tempFilePaths;
						uni.showLoading({
							mask: true,
							title: '加载中'
						})
						for (let i = 0; i < igmFile.length; i++) {
							uni.uploadFile({
								url: that.$config.requestUrl + '/Common/upload',
								header: {
									'tokens': uni.getStorageSync('token')
								},
								formData: {
									accept: 'image',
								},
								filePath: igmFile[i],
								name: 'UploadFile',
								success: (result) => {
									uni.hideLoading()
									let data = JSON.parse(result.data); //微信和头条支持
									console.log(data);
									if (data.code == 0) {
										that.list.push(data.data.path)
										that.idlist.push(data.data.id)
									} else {
										uni.$showMsg(data.msg)
									}

								},
								fail(err) {
									uni.hideLoading()
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.title {
		font-weight: bold;
		padding: 10px;
		font-size: 15px;
	}

	.list {
		padding: 10px;
		display: grid;
		grid-template-columns: 32% 32% 32%;
		grid-column-gap: 1.5%;
		grid-row-gap: 10px;

		image {
			width: 25vw;
			height: 25vw;
		}
	}

	.btn {
		position: fixed;
		bottom: 10px;
		width: 92%;
		left: 50%;
		transform: translate(-50%);
		background: #050B18;
		font-weight: 700;
		color: #fff;
		border-radius: 25px;
		text-align: center;
		padding: 12px 0;
		display: flex;
		align-items: center;
		justify-content: center;
		font-family: "黑体";
		height: auto;
		line-height: normal;

		text {
			font-weight: normal;
		}
	}
</style>